package task

import (
	"gin/cdai"
	cResp "gin/cdai/response"
	"gin/common"
	"net/http"
	"strings"
)

const (
	UrlCreateFaceTask  = "/api/create-face-task"
	UrlCreateVoiceTask = "/api/create-voice-task"
)

func CreateFaceTask(fileName, callbackUrl string) (*cResp.CreateTaskResp, error) {
	res := &cResp.CreateTaskResp{}
	params := map[string]interface{}{
		"file_name":    fileName,    // 文件名
		"callback_url": callbackUrl, // 回调地址
	}
	paramString := common.GetUrlQueriesEncoded(params)
	httpRequest, err := http.NewRequest("POST", cdai.GetApiDomain()+UrlCreateFaceTask, strings.NewReader(paramString))
	if err != nil {
		return res, err
	}
	httpRequest.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	cdai.BuildCredentialHeader(httpRequest, &params)

	client := cdai.GetHttpClient()
	httpResp, err := client.Do(httpRequest)
	if err != nil {
		return res, err
	}
	err = cResp.ParseFromHttpResponse(httpResp, res)
	return res, err
}

func CreateVoiceTask(fileName, callbackUrl string) (*cResp.CreateTaskResp, error) {
	res := &cResp.CreateTaskResp{}
	params := map[string]interface{}{
		"file_name":    fileName,    // 文件名
		"callback_url": callbackUrl, // 回调地址
	}
	paramString := common.GetUrlQueriesEncoded(params)
	httpRequest, err := http.NewRequest("POST", cdai.GetApiDomain()+UrlCreateVoiceTask, strings.NewReader(paramString))
	if err != nil {
		return res, err
	}
	httpRequest.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	cdai.BuildCredentialHeader(httpRequest, &params)

	client := cdai.GetHttpClient()
	httpResp, err := client.Do(httpRequest)
	if err != nil {
		return res, err
	}
	err = cResp.ParseFromHttpResponse(httpResp, res)
	return res, err
}
